/**
 * 
 */
package com.babek.user;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.LookAndFeel;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.babek.business.service.CategoryService;
import com.babek.user.output.CategoryOutput;
import com.babek.user.output.ProductOutput;

/**
 * @author aan
 * 
 */
public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1635582553428446889L;
	private JMenuBar menuBar;
	private JMenuItem productMenuItem;
	private JMenu masterMenu;
	private JMenuItem categoryMenuItem;
	private JMenu settingMenu;
	private JMenuItem brandMenuItem;
	private JMenuItem sizeMenuItem;
	private JMenuItem colorMenuItem;
	private JMenu systemMenu;
	private JMenuItem userMenuItem;
	private JMenuItem exitMenuItem;
	private JToolBar toolbar;
	private JDesktopPane desktopPane;
	private ProductOutput productOutput = null;
	private CategoryOutput categoryOutput=null;
	private static MainFrame instance;
	private static CategoryService categoryService;
	
	
	public static MainFrame getInstance() {
		return instance;
	}


	public MainFrame() {
		// TODO Auto-generated constructor stub
		setTitle("Babek");
		initComponent();
		setSize(new Dimension(900, 700));
		setLocationRelativeTo(null);
		instance=this;
		//ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"application-context.xml"});
		AbstractApplicationContext ctx =
            new ClassPathXmlApplicationContext
            ("classpath:application-context.xml");
           ctx.registerShutdownHook();
		categoryService=(CategoryService) ctx.getBean("categoryService");
		
	}

	private void initComponent() {
		// TODO Auto-generated method stub
		menuBar = new JMenuBar();
		masterMenu = new javax.swing.JMenu();
		productMenuItem = new javax.swing.JMenuItem();
		categoryMenuItem = new javax.swing.JMenuItem();
		settingMenu = new javax.swing.JMenu();
		brandMenuItem = new javax.swing.JMenuItem();
		colorMenuItem = new javax.swing.JMenuItem();
		sizeMenuItem = new javax.swing.JMenuItem();
		systemMenu = new javax.swing.JMenu();
		userMenuItem = new javax.swing.JMenuItem();
		exitMenuItem = new javax.swing.JMenuItem();

		/**
		 * Menu Bar
		 */

		masterMenu.setText("Master");
		productMenuItem.setText("Produk");
		productMenuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				productMenuItemActionPerformed(evt);
			}
		});
		masterMenu.add(productMenuItem);
		categoryMenuItem.setText("Kategori");
		categoryMenuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent evt) {
				// TODO Auto-generated method stub
				categoryMenuItemActionPerformed(evt);
				
			}
		});
		masterMenu.add(categoryMenuItem);
		menuBar.add(masterMenu);

		settingMenu.setText("Setting");
		brandMenuItem.setText("Brand");
		settingMenu.add(brandMenuItem);

		colorMenuItem.setText("Warna");
		settingMenu.add(colorMenuItem);

		sizeMenuItem.setText("Ukuran");
		settingMenu.add(sizeMenuItem);

		menuBar.add(settingMenu);

		systemMenu.setText("System");

		userMenuItem.setText("User");
		systemMenu.add(userMenuItem);

		exitMenuItem.setText("Exit");
		systemMenu.add(exitMenuItem);

		menuBar.add(systemMenu);

		setJMenuBar(menuBar);

		/*
		 * Toolbar
		 */
		JButton insertButton, editButton, removeButton, saveButton, cancelButton;
		toolbar = new JToolBar();
		insertButton = createToolbarButton("add.png", "Add", "Add Data ", "Add");
		editButton = createToolbarButton("edit.png", "Edit", "Edit Data","Edit");
		removeButton = createToolbarButton("delete.png", "Delete","Delete Data", "Delete");
		saveButton = createToolbarButton("save.png", "Save", "Save Data","Delete");
		cancelButton = createToolbarButton("cancel.png", "Cancel","Cancel Data", "Delete");
		toolbar.add(insertButton);
		toolbar.add(editButton);
		toolbar.add(removeButton);
		toolbar.add(saveButton);
		toolbar.add(cancelButton);
		getContentPane().add(toolbar, BorderLayout.NORTH);

		
		desktopPane = new JDesktopPane();
		getContentPane().add(desktopPane, BorderLayout.CENTER);

		pack();

	}
	protected void productMenuItemActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub		
		if(productOutput==null){
			productOutput=new ProductOutput();
			desktopPane.add(productOutput);
		}else{
			productOutput.toFront();
		}		
		productOutput.setVisible(true);

	}
	protected void categoryMenuItemActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		if(categoryOutput==null){
			categoryOutput=new CategoryOutput();
			desktopPane.add(categoryOutput);
		}else{
			categoryOutput.toFront();
		}
		categoryOutput.setVisible(true);
	}

	private JButton createToolbarButton(String icon, String actionCommand,
			String toolTipText, String altText) {
		String imgLocation = "/resources/images/"+icon;
		//System.out.println(" img : "+imgLocation);
		//class.getClass().getResource(name);				
		//System.out.println("Class loader "+cl;
		//URL imageURL = getClass().getResource(imgLocation);
		
		//System.err.println("URL : " + imageURL);
		JButton button = new JButton();
		button.setActionCommand(actionCommand);
		button.setToolTipText(toolTipText);
		//button.addActionListener(this);
		button.setIcon(new ImageIcon(getClass().getResource(imgLocation)));
		/*
		if (imageURL != null) { // image found
			//new javax.swing.ImageIcon(getClass().getResource("/pharmacy/image/blue.JPG"))
			button.setIcon(new ImageIcon(imageURL));
		} else { // no image found
			button.setText(altText);
			System.err.println("Resource not found: " + imgLocation);
		}
		*/

		return button;

	}


	public static CategoryService getCategoryService() {
		return categoryService;
	}


	

	
	
	

}
